package cn.community.care.module.system.controller.admin.basicinfo;

import cn.community.care.module.system.controller.admin.basicinfo.vo.BasicInfoPageReqVO;
import cn.community.care.module.system.controller.admin.basicinfo.vo.BasicInfoRespVO;
import cn.community.care.module.system.controller.admin.basicinfo.vo.BasicInfoSaveReqVO;
import cn.community.care.module.system.dal.dataobject.basicinfo.BasicInfoDO;
import cn.community.care.module.system.service.basicinfo.BasicInfoService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.CommonResult;
import cn.community.care.framework.common.util.object.BeanUtils;
import static cn.community.care.framework.common.pojo.CommonResult.success;

import cn.community.care.framework.excel.core.util.ExcelUtils;

import cn.community.care.framework.operatelog.core.annotations.OperateLog;
import static cn.community.care.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 用户基本信息")
@RestController
@RequestMapping("/user/basic-info")
@Validated
public class BasicInfoController {

    @Resource
    private BasicInfoService basicInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建用户基本信息")
    public CommonResult<Long> createBasicInfo(@Valid @RequestBody BasicInfoSaveReqVO createReqVO) {
        return success(basicInfoService.createBasicInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户基本信息")
    public CommonResult<Boolean> updateBasicInfo(@Valid @RequestBody BasicInfoSaveReqVO updateReqVO) {
        basicInfoService.updateBasicInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户基本信息")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteBasicInfo(@RequestParam("id") Long id) {
        basicInfoService.deleteBasicInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户基本信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BasicInfoRespVO> getBasicInfo(@RequestParam("id") Long id) {
        BasicInfoDO basicInfo = basicInfoService.getBasicInfo(id);
        return success(BeanUtils.toBean(basicInfo, BasicInfoRespVO.class));
    }
    @GetMapping("/get/by/userID")
    @Operation(summary = "获得用户基本信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BasicInfoRespVO> getBasicInfoByUserID(@RequestParam("id") Long id) {
        BasicInfoDO basicInfo = basicInfoService.getBasicInfoByUserID(id);
        return success(BeanUtils.toBean(basicInfo, BasicInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户基本信息分页")
    public CommonResult<PageResult<BasicInfoRespVO>> getBasicInfoPage(@Valid BasicInfoPageReqVO pageReqVO) {
        PageResult<BasicInfoDO> pageResult = basicInfoService.getBasicInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BasicInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户基本信息 Excel")
    @OperateLog(type = EXPORT)
    public void exportBasicInfoExcel(@Valid BasicInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BasicInfoDO> list = basicInfoService.getBasicInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户基本信息.xls", "数据", BasicInfoRespVO.class,
                        BeanUtils.toBean(list, BasicInfoRespVO.class));
    }

}